package cn.school.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.apache.ibatis.cache.Cache;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.UUID;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @author oldliu
 * @since 1.0
 */
public class RedisCacheJson implements Cache {
    private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
    private Jedis redisClient=createRedis();
    private String id;
    private String cls;

    public RedisCacheJson() {
        this(UUID.randomUUID().toString());
        System.out.println("clss==========="+this.cls);

    }
    public RedisCacheJson(final String id) {
        if (id == null) {
            throw new IllegalArgumentException("Cache instances require an ID");
        }
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>MybatisRedisCache:id="+id);
        System.out.println("clss==========="+this.cls);
        this.id = id;
    }

    public String getCls() {
        return cls;
    }

    public void setCls(String cls) {
        this.cls = cls;
    }

    @Override
    public String getId() {
        return this.id;
    }

    @Override
    public int getSize() {

        return Integer.valueOf(redisClient.dbSize().toString());
    }

    @Override
    public void putObject(Object key, Object value) {
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>putObject:"+key+"="+value);
        redisClient.set(JSON.toJSONString(key.toString()), JSON.toJSONString(value));
        System.out.println("key:"+JSON.toJSONString(key));
        System.out.println("value:"+JSON.toJSONString(value));//json数组，不是单个对象
    }

    @Override
    public Object getObject(Object key) {
        System.out.println(key.getClass());
        String s = redisClient.get(JSONObject.toJSONString(key.toString()));
        Object value = JSONArray.parseArray(s);//返回集合对象
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>getObject:"+key+"="+value);
        return value;
    }

    @Override
    public Object removeObject(Object key) {
        return redisClient.expire(JSON.toJSONString(key.toString()),0);
    }

    @Override
    public void clear() {
        System.out.println("clear 被调用了。。。");
        redisClient.flushDB();
        redisClient.close();
    }
    @Override
    public ReadWriteLock getReadWriteLock() {
        return readWriteLock;
    }
    protected  static Jedis createRedis(){

        ///JedisPool pool = new JedisPool( "127.0.0.1",6379);
        GenericObjectPoolConfig g=new GenericObjectPoolConfig();

        JedisPool pool = new JedisPool( new GenericObjectPoolConfig(),"127.0.0.1",6379);
        Jedis j= pool.getResource();
        j.auth("laoliu123");
        //Jedis j= new Jedis("127.0.0.1",6379);
        j.select(7);//选择哪个库

        return j;
    }
}
